require.config({
    baseUrl: '/static',
    paths: {
        jquery: './lib/jquery',
        bootstrap: './lib/bootstrap.min',
        jqueryDataTables: './lib/jqueryDataTables',
        dataTablesBootstrap: './lib/dataTablesBootstrap',
        newIframe: './lib/common/newIframe', // 生成标签页
        tableOption: './lib/common/tableOption', // 列表内容选择 ， 删除等操作
        preventBack: './lib/common/preventBack', //阻止浏览器后退
        validate: './lib/jquery.validate.min',
        messagesZh: './lib/messagesZh',
        validateExtend: './lib/common/validateExtend',
        checkSelect: './lib/common/checkSelect',
        checkOne: './lib/common/checkSingleSelect',
        jqueryUI: './js/jquery-ui',
        ajaxTable: './module/user/userTable',
        tableSearch: './lib/common/tableSearch'

    },

    shim: {
        bootstrap: {
            deps: ['jquery'],
            exports: 'bootstrap'
        },
        validate: {
            deps: ['jquery'],
            exports: 'validate',
        },
        validateExtend: {
            deps: ['jquery', 'validate'],
            exports: 'validateExtend'
        },
        messagesZh: {
            deps: ['jquery', 'validate'],
            exports: 'messagesZh'
        },
        jqueryDataTables: {
            deps: ['jquery'],
            exports: 'jqueryDataTables',
        },

        dataTablesBootstrap: {
            deps: ['bootstrap', 'jqueryDataTables'],
            exports: 'dataTablesBootstrap'
        },
        uploadImg: {
            deps: ['jquery'],
            exports: 'uploadImg'
        },
        checkSelect: {
            deps: ['jquery'],
            exports: 'checkSelect'
        },
        checkOne: {
            deps: ['jquery'],
            exports: 'checkOne'
        },
        jqueryUI: {
            deps: ['jquery'],
            exports: 'jqueryUI'
        },
        ajaxTable: {
            deps: ['jquery', 'jqueryDataTables', 'dataTablesBootstrap'],
            exports: 'ajaxTable'
        },
        tableSearch: {
            deps: ['jquery', 'jqueryDataTables'],
            exports: 'tableSearch'
        }
    }
});

define(['jquery', 'checkSelect', 'checkOne', 'tableSearch', 'bootstrap', 'jqueryDataTables', 'dataTablesBootstrap', 'ajaxTable', 'newIframe', 'tableOption', 'preventBack', 'validate',  'messagesZh', 'validateExtend','jqueryUI'], function($, checkSelect, checkOne, tableSearch) {
    //表单验证
    $('form').validate({
        submitHandler: function (form) {
            if ($("#default_img").find(".img_item").length == 0) {
                return alert("封面必须上传")
            } else {
                if ($("textarea[name='details']").length ==0 || $("textarea[name='details']").val() == '') {
                    return alert("图文详情必须填写")
                }else{
                    form.submit();
                }
            }
        }
    });

    //点击搜索符合条件的
    $("#modalSearch,#addInfos").click(function(){
        $.post(ajaxUrl,{keyword: $("#searchNews").val()},
            function(data){
                data = JSON.parse(data);
                if(data.length > 0){
                    var str = "";
                    $(data).each(function(i, item){
                        if (relation_id.indexOf(item.id) < 0 && item.id!=infoId) {
                            str += '<tr><td><div class="checkbox news-item"><label><input type="checkbox" value="'+item.id+'" data-title="'+item.theme+'"></label></div></td><td>'+item.theme+'</td></tr>';
                        }
                    })
                    $("#info_table tbody").html("").append(str);
                }
            })
    })
    //点击添加关联资讯验证
    $("#confirm").click(function(){
        var check_num = $(".news-list").find(":checked").length;
        console.log(check_num);
        console.log(count_num);
        if (check_num > 0) {
            if (check_num > count_num) {
                alert("只能关联两个资讯");return;
            }
            var str = "";
            $(".news-list .news-item").each(function(i, item){
                console.log('item', item)
                if($(item).find("input").is(":checked")){
                    var id = $(item).find("input").val()
                    var news = $(item).find("input").data('title');
                    relation_id.push(id);
                    str += '<div class="news"><label for="" class="control-label">'+news+'</label><img src="'+imgUrl+'deleteIcon.png" class="news-del" alt="" data-did="'+id+'">'+
                    '<input type="hidden" name="news[]" value="'+id+'"></div>'
                }
            })
            $("#newsList").append(str);
            if (count_num == check_num) {
                $("#addInfos").attr("disabled", true);
                count_num = 0;
            }else{
                count_num = count_num - check_num;
            }
        }else{
            alert("请选择需要关联的资讯");return;
        }
        //隐藏模态框
        $('#myModal').modal('hide')
        //清空模态框数据
        $("#searchNews").val('');
        // $(".news-list").empty();
    })

    //删除关联资讯
    $("body").on("click", ".news-del", function(){
        var did = $(this).data('did');
        //删除relation_id数组中的did对应值
        for (var i = 0; i < relation_id.length; i++) {
            if (relation_id[i] == did) {
                relation_id.splice(i, 1);
                break;
            }
        };
        $(this).parents(".news").remove();
        count_num++;
        $("#addInfos").removeAttr("disabled", true);
    })

    // 预览图片
    $("body").on("click", ".previewImg", function() {
        var imgs = $(this).data("src").split(",");

        console.log('click', imgs)
        $("img#contract-img").attr("src", $(this).data("src"))
        $("#img_modal").modal('show')
    })

    //图片上传
    $(function() {
        // 如果图片已经到达了上传的最大张数，添加图片按钮隐藏
        $(".img-box").each(function(index, val) {
            console.log($(val).data("num"), $(val).find(".img_item").length)
            if (parseInt($(val).data("num")) == $(val).find(".img_item").length) {
                $(val).find(".add_img").hide()
            }
        })
        var maxSum = 1; //最多上传的图片张数
        var goods_img_num = $(".img_item").length;

        // 上传图片
        $('body').on('click', '.add_img', function() {
            name = $(this).attr('name');
            maxSum = $(this).parents(".img-box").data("num");
            console.log('maxSum', maxSum)
            $(this).siblings('input[type="file"]').click();
        });
        $('body').on('change', 'input[type="file"]', function(e) {
            var _this = $(this);
            $('.mud').find('div').text("正在上传...");
            $(".mud").show();
            var add_img = $(this).siblings('.add_img');
            var file = e.target.files;
            if (file) {
                var reader = new FileReader();
                reader.onload = function(evt) {
                    $.post(uploadUrl, {
                        'imageData': evt.target.result
                    }, function(json) {
                        console.log(json);
                        json = $.parseJSON(json);
                        var tem = '<div class="img_item">' +
                        '<img src="' + json.url + '">' +
                        '<input type="hidden" name="' + name + '" value="' + json.url + '" >' +
                        '<a class="del_img del">×</a>' +
                        '</div>';
                        add_img.before(tem);
                        goods_img_num++;
                        if (_this.data("sign") == "single" || maxSum == goods_img_num) {
                            add_img.next('label').remove();
                            _this.siblings(".add_img").hide();
                        } //单图上传或者达到了上传的数量
                        $('.mud').find('div').text("上传成功");
                        setTimeout(function() {
                            $(".mud").hide();
                        }, 1000);
                    });
                };
                reader.readAsDataURL(file[0]);
            }
        });


        //删除分类图片
        $('body').on('click', 'a.del.del_img', function() {
            goods_img_num--;
            $(this).parents('.img_item').remove();
            $(".add_img").show();
            $(".mud").show().find('div').text("删除成功");
            setTimeout(function() {
                $(".mud").hide();
            }, 1000);
        });
    })

})